package shop.ynubeggar.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import shop.ynubeggar.model.valueobject.AddressVO;
import shop.ynubeggar.request.AddressRequest;
import shop.ynubeggar.service.AddressService;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 外卖-收发货地址表 前端控制器
 * </p>
 *
 * @author LiuQ
 * @since 2022-09-18
 */
@Api(tags = "地址")
@RestController
@RequestMapping("/api/user/v1/address/")
public class AddressController {

    @Resource
    AddressService addressService;

    @ApiOperation(value = "新增", notes = "新增收货地址")
    @PostMapping("")
    public void newAddress(@ApiParam(value = "收货地址", required = true)
                           @RequestBody AddressRequest addressRequest) {
        addressService.createAddress(addressRequest);
    }

    @ApiOperation(value = "所有地址", notes = "查询所有收货地址")
    @GetMapping("")
    public List<AddressVO> listAllAddress() {
        return addressService.findAllAddress();
    }

    @ApiOperation(value = "详情", notes = "根据地址对象id查找地址详情")
    @GetMapping("{id}")
    public AddressVO detail(@ApiParam(value = "地址id", required = true)
                            @PathVariable(value = "id") long id) {
        return addressService.queryDetail(id);
    }

    @ApiOperation(value = "删除", notes = "根据地址对象id删除地址")
    @DeleteMapping("{id}")
    public void delAddress(@ApiParam(value = "地址id", required = true)
                           @PathVariable(value = "id") long id) {
        addressService.delAddress(id);
    }

    @ApiOperation(value = "修改", notes = "根据地址对象id删除地址")
    @PutMapping("{id}")
    public void updateAddress(@ApiParam(value = "地址id", required = true)
                              @PathVariable(value = "id") long id,
                              @ApiParam(value = "收货地址")
                              @RequestBody AddressRequest addressRequest) {
        addressService.updateAddress(id, addressRequest);
    }

}

